package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.GzhPushRecordBo;
import org.dromara.system.domain.vo.GzhPushRecordVo;
import org.dromara.system.domain.GzhPushRecord;
import org.dromara.system.mapper.GzhPushRecordMapper;
import org.dromara.system.service.IGzhPushRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订阅推送记录Service业务层处理
 *
 * @author Lion Li
 * @date 2024-06-28
 */
@RequiredArgsConstructor
@Service
public class GzhPushRecordServiceImpl implements IGzhPushRecordService {

    private final GzhPushRecordMapper baseMapper;

    /**
     * 查询订阅推送记录
     */
    @Override
    public GzhPushRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订阅推送记录列表
     */
    @Override
    public TableDataInfo<GzhPushRecordVo> queryPageList(GzhPushRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GzhPushRecord> lqw = buildQueryWrapper(bo);
        Page<GzhPushRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订阅推送记录列表
     */
    @Override
    public List<GzhPushRecordVo> queryList(GzhPushRecordBo bo) {
        LambdaQueryWrapper<GzhPushRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GzhPushRecord> buildQueryWrapper(GzhPushRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GzhPushRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, GzhPushRecord::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyword()), GzhPushRecord::getKeyword, bo.getKeyword());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectUnit()), GzhPushRecord::getProjectUnit, bo.getProjectUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getPushStatus()), GzhPushRecord::getPushStatus, bo.getPushStatus());
        return lqw;
    }

    /**
     * 新增订阅推送记录
     */
    @Override
    public Boolean insertByBo(GzhPushRecordBo bo) {
        GzhPushRecord add = MapstructUtils.convert(bo, GzhPushRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订阅推送记录
     */
    @Override
    public Boolean updateByBo(GzhPushRecordBo bo) {
        GzhPushRecord update = MapstructUtils.convert(bo, GzhPushRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GzhPushRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订阅推送记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
